package com.zzptc.sky.bdsjws.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.zzptc.sky.bdsjws.R;
import com.zzptc.sky.bdsjws.fragments.base.BaseFragment;
import com.zzptc.sky.bdsjws.services.MyDownLoadService;
import com.zzptc.sky.bdsjws.utils.MyUtil;
import com.zzptc.sky.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/16 0016.
 */

public class UpdateVersionFragment extends BaseFragment {
    @BindView(R.id.tv_update)
    TextView tv_Update;
    Unbinder unbinder;
    @BindView(R.id.btn_yes)
    Button btnYes;
    @BindView(R.id.btn_no)
    Button btnNo;
    Unbinder unbinder1;

    public String getMsg() {
        Bundle bundle = getArguments();
        String msg = bundle.getString("msg");
        return msg;
    }

    @Override
    public void initDataView(View v) {
        //更新TextView getArgument
        Bundle bundle = getArguments();
        VersionInfo versionInfo = (VersionInfo) bundle.get("versioninfo");

//        tv_Update.setText(versionInfo.getInfo());
        if (versionInfo != null) {
            tv_Update.setText(versionInfo.getInfo());
//            String apkurl=versionInfo.getApkurl();
            btnNo.setVisibility(View.VISIBLE);//使取消按钮出来
        } else {
            tv_Update.setText(getMsg());
        }
    }

    @Override
    public int getContentXml() {
        return R.layout.update_version_dialog;
    }

    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }

    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
//        String apkurl=versionInfo.getApkurl();
        bundle.putSerializable("versioninfo", versionInfo);
        updateVersionFragment.setArguments(bundle);
        return updateVersionFragment;
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder = ButterKnife.bind(this, rootView);
//        return rootView;
//    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if(unbinder!=null) {
            unbinder.unbind();
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder1 = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.btn_yes, R.id.btn_no})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_yes:
                Bundle bundle = getArguments();
                VersionInfo versionInfo = (VersionInfo) bundle.get("versioninfo");
                if (versionInfo != null) {
                    Intent intent=new Intent();
                    intent.setClass(getActivity(), MyDownLoadService.class);
                    MyDownLoadService myDownLoadService=new MyDownLoadService();
                    myDownLoadService.createNotification();
                    intent.putExtra("apkurl",versionInfo.getApkurl());
                    getActivity().startService(intent);
                }
                dismiss();
                break;
            case R.id.btn_no:
                dismiss();
                break;
        }
    }
}
